package com.milhouz.bomberrokon.model;

import com.milhouz.bomberrokon.events.BomberMoveEvent;
import com.milhouz.bomberrokon.events.Event;

public class GameRound {

	// Game Data
	private int nbPlayers;

	// Game Party Data
	private int nbRounds;
	private int[] gameVictories;
	private boolean stopWhenPlayerKilled = true;
	private boolean stopWhen1BomberStay = true;
	private boolean stopWhenTimesUp = true;

	// Current game round Data
	private int currentRound;
	private boolean finished = false;
	private boolean started = false;
	private boolean playerLost = false;
	private Timer timer;
	private int[] bombersAlive;
	private boolean endRoundShowed = false;

	public GameRound(int time, int nbPlayers, int nbRounds, int currentRound, int[] gameVictories) {
		timer = new Timer(time);
		this.nbRounds = nbRounds;
		this.nbPlayers = nbPlayers;
		this.currentRound = currentRound;
		this.gameVictories = gameVictories;
		bombersAlive = new int[nbPlayers];
	}

	// Game Party Methods
	public void finishRound() {
		currentRound++;
		for (int i = 0; i < bombersAlive.length; i++) {
			if (bombersAlive[i] != -1) {
				gameVictories[i] = gameVictories[i] + 1;
			}
		}
	}

	public int getVictory(int idBomber) {
		return gameVictories[idBomber];
	}

	public int[] getVictories() {
		return gameVictories;
	}

	public boolean isFinishedParty() {
		return currentRound == nbRounds;
	}

	// Game Round Methods
	public void start() {
		started = true;
		timer.start();
	}

	public void pause() {
		timer.pause();
	}

	public void resume() {
		timer.resume();
	}

	public boolean isFinished() {
		return isTimesUp() || finished;
	}

	public Timer getTimer() {
		return timer;
	}

	public boolean isStarted() {
		return started;
	}

	private boolean isTimesUp() {
		return (stopWhenTimesUp && !getTimer().isRunning());
	}

	public boolean isEndRoundShowed() {
		return endRoundShowed;
	}

	public void setEndRoundShowed(boolean endRoundShowed) {
		this.endRoundShowed = endRoundShowed;
	}

	public void processEvent(Event event) {
		switch (event.type) {
		case Event.KILL_BOMBER_EVENT:
			// Receive Bomber killed event
			int idBomber = event.getIntParam(BomberMoveEvent.ID_BOMBER_PARAMETER);
			bombersAlive[idBomber] = -1;
			if (idBomber == 0) {
				// Player killed
				if (stopWhenPlayerKilled) {
					playerLost = true;
					finished = true;
				}
			} else {
				// Bomber bot killed
				if (stopWhen1BomberStay && isOnlyOnePlayer()) {
					finished = true;
				}
			}
			break;
		}
	}

	private boolean isOnlyOnePlayer() {
		int nbDead = 0;
		for (int i = 0; i < bombersAlive.length; i++) {
			if (bombersAlive[i] == -1) {
				nbDead++;
			}
		}
		return ((nbPlayers - nbDead) == 1);
	}

	public int getNbPlayers() {
		return nbPlayers;
	}

	public int getCurrentRound() {
		return currentRound;
	}

	public boolean isPlayerLost() {
		return playerLost;
	}

}